package xmlrpc.spring.xmlrpc.controller;

import org.apache.xmlrpc.webserver.XmlRpcServletServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xmlrpc.spring.xmlrpc.server.XmlRpcServletServerFactoryBean;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class RpcController {

    @Resource
    private XmlRpcServletServerFactoryBean xmlRpcServletServerFactory;

    @RequestMapping("/hello")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    //rpc 调用的路径（代替servlet）
    @RequestMapping(value = "/handler")
    @ResponseBody
    public void stationHandler(HttpServletRequest request, HttpServletResponse response) {
        try {

            XmlRpcServletServer server = (XmlRpcServletServer) xmlRpcServletServerFactory.getObject();
            server.execute(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
